<?php
/*------------------------------------------------------------------------------
this file is part of Web Application Kit http://webappkit.net
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/

/**
*
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 
*/
class wakTemplateStorage extends wakService {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	*/
	function service_version() {
		$this->__construct();
	}/**/
	
	/**
	* "real" constructor
	* @access
	*/
	function __construct() {
		parent::__construct();
		$this->deb=&wak::getservice('wakdebug');
		$lib=&wak::getservice('waklib');
		$lib->needclass('waktemplate');
		$lib->needClass('foldernode');
		$lib->needClass('filenode');
		$this->folders[]=&new foldernode(WAK_CORE_PATH.'services.data/views');
		$this->folders[]=&new foldernode('app/services.data/views');
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* get template by id
	* @param string $id
	* @access public
	* @return wakTemplate
	*/
	function & getById($id) {
		// getting into self memory cache
		/*if (isset($this->templates[$id]))
			return $this->templates[$id];*/
		// getting from cache.store
		if ($tpl=$this->getFromCache($id))
			return $tpl;
		$tpl=false;
		// searching for file
		//$id=str_replace('.','\.',$id);
		$regex="^($id)\.([a-zA-Z_-]+)\.tpl\$";
		if (!$results=$this->_regexSearchFolders($regex)) {
			$this->deb->notice("couldn't find template $id");
			return $tpl;
		}
		$fileNode=&$results[0];
		$content=$fileNode->read();
		// searching for engine type
		$search=$fileNode->regexSelfName($regex);
		$engine=isset($search[2])?$search[2]:null;
		// building template instance
		$tpl=$this->_buildTemplate($id,$engine,$content);
		return $tpl;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var array template self memory cache
	*/
	var $templates=array();
	/**
	* @var array configuration data
	*/
	var $cfg=array(
		'use_cache'=>true
	);
	
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @deprecated
	* @param
	* @access private
	* @return 
	*/
	function & _buildTemplate($id,$engine,&$content) {
		// building template
		$tpl=&new wakTemplate();
		$tpl->setId($id);
		$tpl->setEngine($engine);
		$tpl->setContent($content);
		// setting into self memory cache
		//$this->templates[$id]=&$tpl;
		// saving into cache.store
		if ($this->cfg['use_cache'])
			$this->setIntoCache($tpl);
		//echo wakapi::vardump($tpl);
		return $tpl;
	}/**/
	
	/**
	* builds template instance
	* @param string $engine
	* @param string &$content
	* @access private
	* @return object
	* /
	function & _buildTemplate($engine,&$content) {
		// getting engine
		$engine=&wak::getservice('waktemplateengine',$engine);
		$tpl=&$engine->buildTemplate($content);
		return $tpl;
	}/**/
	
	/**
	* regex searches into folders
	* @param string $regex
	* @access private
	* @return array
	*/
	function _regexSearchFolders($regex) {
		for ($n=0; $n<count($this->folders); $n++) {
			if ($results=$this->folders[$n]->regexSearchName($regex,true))
				return $results;
		}
		return false;
	}/**/
	
	/**
	* get template instance from serialized file into cache.store
	* @param string $id
	* @access private
	* @return false|object
	*/
	function & getFromCache($id) {
		$false=false;
		// is caching used ?
		if (!$this->cfg['use_cache'])
			return $false;
		// getting cache foldernode instance
		$cache=&new folderNode('tmp/cache/templates');
		// searching for cached template
		if (!$fileNodes=$cache->regexSearchName('^'.$id)) {
			return $false;
		}
		// getting first result
		$node=$fileNodes[0];
		// getting engine name from file name
		$search=$node->regexSelfName('([a-z]+)\.txt$');
		if (!$engineName=$search[1]) {
			return $false;
		}
		//echo $engineName;
		// getting engine service instance
		// this should define required classes
		if (!$engine=&wak::getservice('waktemplateengine',$engineName))
			return $false;
		//echo wakapi::vardump($engine);
		// getting cached file's content
		$serialized=$node->read();
		// unserializing, string to instance
		$tpl=unserialize($serialized);
		// returning template instance
		//trigger_error('got template '.$id.' from cache');
		return $tpl;
	}/**/
	
	/**
	* saves template to a serialized file into cache.store
	* @param
	* @access private
	* @return bool
	*/
	function setIntoCache(&$tpl) {
		$id=$tpl->get('id');
		$engine=$tpl->get('engine');
		$node=&new filenode('tmp/cache/templates/'.$id.'.'.$engine.'.txt');
		$serialized=serialize($tpl);
		$ok=$node->write($serialized);
		if ($ok)
			trigger_error('saved template '.$id.' into cache');
		else
			trigger_error('failed to save template '.$id.' into cache');
		return $ok;
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
